package ggaviator

import (
	"sync"
	"wzgames/internal/model/mcomm"
	"wzgames/internal/services/dila"
	"wzgames/pkg/actor/actor"
	"wzgames/pkg/actor/actorservice"
)

// 玩家 投注 CashOut 取消投注 动作记录和结算

type Counter struct {
	mu sync.Mutex
	pp *actor.PID

	roundId int64
	mp      map[uint64]int8
}

func NewCounter(roundId int64, pp *actor.PID) *Counter {
	return &Counter{
		roundId: roundId,
		pp:      pp,
		mp:      make(map[uint64]int8),
	}
}

func (ct *Counter) Action(betId uint64, tp int8) {
	ct.mu.Lock()
	ct.mp[betId] = tp
	ct.mu.Unlock()
}

func (ct *Counter) Settle(gameId int32, maxMultiplier float64) {
	ct.mu.Lock()
	defer ct.mu.Unlock()
	for k, v := range ct.mp {
		if v != 1 {
			continue
		}
		dila.SendPlayStateWithMultiplier(k, mcomm.GameAction_GameLoseSettle, gameId, ct.roundId, maxMultiplier)
	}
	actorservice.Send(ct.pp, RoundCounterRemove{RoundId: ct.roundId})
}

func (ct *Counter) Clear() {
	ct.mu.Lock()
	defer ct.mu.Unlock()
	ct.mp = make(map[uint64]int8)
}
